package team.knowers.qzh.qzhadmin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhadmin.dao.entity.CourseDO;
import team.knowers.qzh.qzhadmin.dao.entity.SectionDO;
import team.knowers.qzh.qzhadmin.dao.entity.UserDO;
import team.knowers.qzh.qzhadmin.dao.mapper.UserMapper;
import team.knowers.qzh.qzhadmin.dto.req.PageUserReqDTO;
import team.knowers.qzh.qzhadmin.dto.resp.PageUserMessageRespDTO;
import team.knowers.qzh.qzhadmin.dto.resp.UserMessageRespDTO;
import team.knowers.qzh.qzhadmin.service.CourseAdminService;
import team.knowers.qzh.qzhadmin.service.SectionAdminService;
import team.knowers.qzh.qzhadmin.service.UserAdminService;

import java.util.List;

@Service
@AllArgsConstructor
public class UserAdminServiceImpl extends ServiceImpl<UserMapper, UserDO> implements UserAdminService {
    private final CourseAdminService courseAdminService;
    private final SectionAdminService sectionAdminService;
    @Override
    public String ban(String username) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class).eq(UserDO::getUsername, username)
                .eq(UserDO::getDelFlag, 0);
        UserDO userDO = baseMapper.selectOne(queryWrapper);
        userDO.setRole(1);
        LambdaUpdateWrapper<UserDO> updateWrapper = Wrappers.lambdaUpdate(UserDO.class).eq(UserDO::getUsername, username)
                .eq(UserDO::getDelFlag, 0)
                .set(UserDO::getRole, 1);
        baseMapper.update(userDO, updateWrapper);
        return (username + "已被封禁");
    }

    @Override
    public String relieve(String username) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class).eq(UserDO::getUsername, username)
                .eq(UserDO::getDelFlag, 0);
        UserDO userDO = baseMapper.selectOne(queryWrapper);
        userDO.setRole(0);
        LambdaUpdateWrapper<UserDO> updateWrapper = Wrappers.lambdaUpdate(UserDO.class).eq(UserDO::getUsername, username)
                .eq(UserDO::getDelFlag, 0)
                .set(UserDO::getRole, 0);
        baseMapper.update(userDO, updateWrapper);
        return (username + "已被解除封禁");
    }

    @Override
    public IPage<PageUserMessageRespDTO> showUser(PageUserReqDTO req) {
        if(req.getId()==null&&req.getNickname()==null&&req.getUsername()==null&&req.getRole()==null)
        {
            LambdaQueryWrapper<UserDO> queryWrapper=Wrappers.lambdaQuery(UserDO.class).eq(UserDO::getDelFlag, 0);;
            IPage<UserDO> userDOIPage=baseMapper.selectPage(req,queryWrapper);
            return userDOIPage.convert(each-> BeanUtil.toBean(each,PageUserMessageRespDTO.class));
        }
        else {
            LambdaQueryWrapper<UserDO> queryWrapper = new LambdaQueryWrapper<>();
            if(req.getId()!=null)
            {
                queryWrapper.like(UserDO::getId,"%"+req.getId()+"%");
            }
            if(req.getNickname()!=null)
            {
                queryWrapper.like(UserDO::getNickname,"%"+req.getNickname()+"%");
            }
            if(req.getUsername()!=null)
            {
                queryWrapper.like(UserDO::getUsername,"%"+req.getUsername()+"%");
            }
            if(req.getRole()!=null)
            {
                queryWrapper.like(UserDO::getRole,"%"+req.getRole()+"%");
            }
            IPage<UserDO> userDOIPage=baseMapper.selectPage(req,queryWrapper);
            return userDOIPage.convert(each-> BeanUtil.toBean(each,PageUserMessageRespDTO.class));
        }
    }
    @Override
    public UserMessageRespDTO showUser(String username) {
        LambdaQueryWrapper<UserDO> queryWrapper = Wrappers.lambdaQuery(UserDO.class).eq(UserDO::getUsername, username)
                .eq(UserDO::getDelFlag, 0);
        UserDO userDO = baseMapper.selectOne(queryWrapper);
        UserMessageRespDTO userMessageRespDTO = new UserMessageRespDTO();

        if (userDO != null) {
            userMessageRespDTO.setFans(userDO.getFans());
            userMessageRespDTO.setLikes(userDO.getLikes());
            userMessageRespDTO.setUserId(userDO.getId());
            LambdaQueryWrapper<CourseDO> queryWrapper1 = Wrappers.lambdaQuery(CourseDO.class).eq(CourseDO::getUserId, userDO.getId())
                    .eq(CourseDO::getDelFlag, 0);
            List<CourseDO> courseDOList = courseAdminService.getBaseMapper().selectList(queryWrapper1);
            LambdaQueryWrapper<SectionDO> queryWrapper2=Wrappers.lambdaQuery(SectionDO.class).eq(SectionDO::getAuthorId,userDO.getId())
                    .eq(SectionDO::getDelFlag,0);
            List<SectionDO> sectionDOS=sectionAdminService.getBaseMapper().selectList(queryWrapper2);
            userMessageRespDTO.setCourseNumber(courseDOList.size());
            userMessageRespDTO.setVersionNumber(sectionDOS.size());
        }

        return userMessageRespDTO;
    }

}
